<template>
    <div>
        <!-- 搜索栏 -->
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="智能体">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.name"
                            placeholder="请输入智能体名称"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="拥有者">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.user"
                            placeholder="请输入用户信息"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="状态">
                    <div class="w-[120px]">
                        <el-select
                            v-model="queryParams.is_disable"
                            placeholder="全部"
                        >
                            <el-option value="" label="全部"/>
                            <el-option value="0" label="正常"/>
                            <el-option value="1" label="禁用"/>
                        </el-select>
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPaging">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <!-- 表格栏 -->
        <el-card v-loading="pager.loading" shadow="never" class="!border-none">
            <el-table :data="pager.lists" size="large" class="mt-4">
                <el-table-column label="ID" prop="id" min-width="60" />
                <el-table-column label="图标" min-width="100">
                    <template #default="{ row }">
                        <el-avatar :size="50" :src="row.logo"/>
                    </template>
                </el-table-column>
                <el-table-column label="智能体名称" prop="name" min-width="150" show-tooltip-when-overflow />
                <el-table-column label="拥有者" prop="user" min-width="120">
                    <template #default="{ row }">
                        <el-popover placement="right" width="220px" trigger="hover">
                            <div class="flex items-center">
                                <span class="mr-4">头像: </span>
                                <el-avatar :size="50" :src="row?.user?.avatar" />
                            </div>
                            <div class="mt-[20px]">
                                <span class="mr-4">昵称: </span>
                                <span>{{ row?.user?.nickname }}</span>
                            </div>
                            <div class="mt-[20px]">
                                <span class="mr-4">编号: </span>
                                <span>{{ row?.user.sn }}</span>
                            </div>
                            <template #reference>
                                {{ row?.user?.nickname }}
                            </template>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column label="使用模型" prop="model" min-width="150">
                    <template #default="{ row }">
                        <el-tag type="info">{{ row?.model || '-' }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="知识检索" prop="search" min-width="110">
                    <template #default="{ row }">
                        <el-popover placement="right" width="180px" trigger="hover">
                            <div class="mb-[10px]">检索模式: &nbsp;{{ row?.search_mode }}</div>
                            <div class="mb-[10px]">检索引用: &nbsp;{{ row?.search_tokens }}tokens</div>
                            <div class="mb-[10px]">检索重排: &nbsp;{{ row?.ranking_status ? '是' : '否' }}</div>
                            <div>问题优化: &nbsp;{{ row?.ask_optimize ? '是' : '否' }}</div>
                            <template #reference>
                                <el-tag type="info">{{ row?.search_mode }}</el-tag>
                            </template>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column label="知识库关联" prop="role" min-width="110">
                    <template #default="{ row }">
                        <el-tag>{{ row?.id }} 个</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="状态" prop="is_disable" min-width="80">
                    <template #default="{ row }">
                        <div @click="handleDisable(row.id, row.is_disable)">
                            <el-tag v-if="row.is_disable === 0" class="cursor-pointer">正常</el-tag>
                            <el-tag v-else type="danger" class="cursor-pointer">禁用</el-tag>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="create_time" min-width="180" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['agent:robot:smarter:detail']"
                            type="primary"
                            link
                            @click="emits('listen', 'editor', row.id)"
                        >
                            查看
                        </el-button>
                        <el-button
                            v-perms="['agent:robot:smarter:delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <paging v-model="pager" @change="queryLists" />
            </div>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import robotSmarterApi from '@/api/agent/robot/smarter'

const emits = defineEmits(['listen'])

// 查询参数
const queryParams = reactive({
    name: '',
    user: '',
    is_disable: ''
})

// 分页查询
const { pager, queryLists, resetParams, resetPaging } = usePaging({
    fetchFun: robotSmarterApi.lists,
    params: queryParams
})

/**
 * 处理删除
 *
 * @param {number} id
 * @returns {Promise<void>}
 */
const handleDelete = async (id: number): Promise<void> => {
    feedback.prompt('请输入删除原因', '删除智能体', {
        inputType: 'textarea',
        inputPattern: /.+/,
        inputErrorMessage: '删除原因不能为空',
    }).then(async (v) => {
        await robotSmarterApi.delete(id, v.value)
        feedback.msgSuccess('删除成功')
        await queryLists()
    }).catch(() => {})
}

/**
 * 处理状态
 *
 * @param {number} id
 * @param {number} status
 */
const handleDisable = async (id: number, status: number) => {
    if (status === 0) {
        feedback.prompt('请输入禁用原因', '禁用智能体', {
            inputType: 'textarea',
            inputPattern: /.+/,
            inputErrorMessage: '禁用原因不能为空',
        }).then(async (v) => {
            await robotSmarterApi.disable(id, v.value)
            feedback.msgSuccess('禁用成功')
            await queryLists()
        }).catch(() => {})
    } else {
        feedback.confirm('确定要解禁该智能体吗?')
            .then(async () => {
                await robotSmarterApi.disable(id)
                feedback.msgSuccess('解禁成功')
                await queryLists()
            }).catch(() => {})
    }
}

onMounted(async () => {
    await queryLists()
})
</script>
